import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import store from './redux';

// 首次加载
ReactDOM.render(<App />, document.getElementById('root'));

// 当 store 中的 state 发送变化时，更新这个函数，这个函数会触发视图更新，使用新的 state 值来修改页面显示的内容
store.subscribe(() => {
    ReactDOM.render(<App />, document.getElementById('root'));
});

// 页面离开时，做一些事情
window.onbeforeunload = function () {
    window.sessionStorage.setItem(
        'redux-state',
        JSON.stringify(store.getState())
    );
};

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
